package com.tgy.algorithm.base.other;

import java.util.Arrays;

public class _矩阵相乘 {

    /**
     *
     * 2x3
     * [
     *    [1,2,3]
     *    [2,2,2]
     * ]
     *
     * 3x2
     * [
     *    [1,2]
     *    [1,2]
     *    [1,2]
     * ]
     *
     * @param one
     * @param two
     * @return
     */
    public static int[][] matrixMultiply(int[][] one, int[][] two) {

        int[][] ints = new int[one.length][two[0].length];
        for (int i = 0;i < one.length;i++) {
            int[] oneLine = one[i];
            // x控制列
            for (int x = 0; x < two[0].length; x++) {
                int sum = 0;
                for (int j = 0; j < oneLine.length; j++) {
                    sum += (oneLine[j] * two[j][x]);
                }
                ints[i][x] = sum;
            }
        }


        return ints;
    }

    public static void main(String[] args) {
        // 2x3
        /**
         * {
         *     {1,2,3}
         *     {2,2,2}
         * }
         */
        int[][] one = {{1,2,3},{2,2,2}};
        // 3x2
        /**
         * {
         *     {1,1},
         *     {2,2},
         *     {2,1}
         * }
         */
        int[][] two = {{1,1},{2,2},{2,1}};
        int[][] ints = matrixMultiply(one, two);
        for (int[] anInt : ints) {
            System.out.println(Arrays.toString(anInt));
        }
    }
}
